<template>
  <div class="register">
    <h2>欢迎您，请注册</h2>
    <van-form @submit="onSubmit">
      <van-field
        class="txt1"
        @failed="onFailed"
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="请输入11位手机号"
        @blur="onBlur"
        :rules="[
          {
            required: true,
            pattern: /^1[3-9]\d{9}$/,
            message: '请填写11位手机号码',
          },
        ]"
      />
      <van-field
        class="txt1"
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="请设置4位数密码"
        :rules="[
          { required: true, pattern: /^\d{4}$/, message: '请填写四位密码' },
        ]"
      />
      <section>
        <van-field
          class="txt2"
          v-model="veryfiycode"
          name="用户名"
          label="验证码"
          placeholder="请输入短信验证码"
          :rules="[
            {
              required: true,
              pattern: /^\d{5}$/,
              message: '请填写5位验证码',
            },
          ]"
        />
        <van-button
          class="btn"
          color="#f66"
          type="primary"
          size="small"
          v-model="code"
          @click="sendCode()"
          >发送验证码</van-button
        >
      </section>
      <div style="margin: 16px">
        <van-button
          class="btn1"
          color="#f66"
          round
          block
          type="info"
          native-type="submit"
          @click="myReg()"
          >注册</van-button
        >
      </div>
    </van-form>
    
  </div>
</template>

<script>
import { fun_sendcode } from "../api/index.js";
import { fun_checkPhone } from "../api/index.js";
import Vue from "vue";
import { Form } from "vant";
import { Field } from "vant";
import { Button } from "vant";
import { Toast } from "vant";

Vue.use(Button);
Vue.use(Form);
Vue.use(Field);
Vue.use(Toast);
export default {
  data() {
    return {
      username: "",
      password: "",
      veryfiycode: "",
      code: "",
      // disabled: true,
    
    };
  },
  methods: {
    onFailed() {},
    onBlur() {
      if (this.username != "")
        fun_checkPhone(this.username).then((res) => {
          if (res.data.code == 200) {
            return;
          } else {
            Toast("该用户已被注册");
          }
        });
    },
    myReg() {
      if ((this.username == "") & (this.password == "") & (this.code == "")) {
        Toast("请填写信息");
        return;
      }
    },
    sendCode() {
      fun_sendcode(this.username).then((res) => {
        console.log(res.data.data);
      });
    },
    onSubmit(values) {
      console.log("submit", values);
      this.$store.dispatch("UP_REG", {
        tel: this.username,
        password: this.password,
      });
      if (this.$store.state.up_reg.text) {
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style scoped>
.register {
  padding: 5.5556rem 1.1111rem;
}
h2 {
  margin-bottom: 2.7778rem;
  text-align: center;
}
.txt1 {
  margin-bottom: 1.1111rem;
  border-radius: 2.7778rem;
}
.txt2 {
  margin-bottom: 1.1111rem;
  border-radius: 2.7778rem;
  width: 70%;
}
section {
  position: relative;
  height: 4.4444rem;
}
.btn {
  border-radius: 1.3889rem;
  position: absolute;
  right: 0;
  top: 0.2778rem;
}

</style>